package com.lancoo.klgcourseware.ui.fragment.operate;

import androidx.recyclerview.widget.LinearLayoutManager;

import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.lancoo.klgcourseware.R;
import com.lancoo.klgcourseware.entity.ExpressPracticeModel;
import com.lancoo.klgcourseware.entity.WordPracticeModel;
import com.lancoo.klgcourseware.entity.bean.SentenceLineTrainBean;
import com.lancoo.klgcourseware.entity.bean.SentenceTrainBean;
import com.lancoo.klgcourseware.entity.bean.WordTrainBean;
import com.lancoo.klgcourseware.ui.adapter.SentenceTrainAdapter;
import com.lancoo.klgcourseware.ui.fragment.basicTrain.BasicTrainBaseFragment;
import com.lancoo.klgcourseware.ui.widget.MaxHeightRecyclerView;
import com.lancoo.klgcourseware.utils.KlgToolUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by pengmi on 2019/8/28 17:11
 * Description:
 */
public class AlertUsageTrainOperateFragment extends AlertBaseFragment {



    @Override
    protected int getContentViewId() {
        return R.layout.klg_fragment_alert_usage_train;
    }

    @Override
    protected void initView(View view) {
        if(getContext()==null||getArguments()==null){
            return;
        }
        List<ExpressPracticeModel> expressPracticeModelList=getArguments().getParcelableArrayList(BasicTrainBaseFragment.KEY_USAGE_TRAIN);
        ExpressPracticeModel expressPracticeModel = expressPracticeModelList.get(0);
        MaxHeightRecyclerView recyclerView = view.findViewById(R.id.alert_recycler_view);
        int screenHeight=KlgToolUtils.getHeightInPx(getContext());
        if(screenHeight<1920){
            recyclerView.setmMaxHeight(KlgToolUtils.dip2px(getContext(),100));
        }else if(KlgToolUtils.getHeightInPx(getContext())<=2000){
            recyclerView.setmMaxHeight(KlgToolUtils.getHeightInPx(getContext())/5);
        }else{
            recyclerView.setmMaxHeight(KlgToolUtils.getHeightInPx(getContext())/4);
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        view.findViewById(R.id.tv_i_know).setOnClickListener(this);
        int startIndex=1;
        List<SentenceLineTrainBean> sentenceLineTrainBeanList =new ArrayList<>();
        for (WordPracticeModel wordPracticeModel:expressPracticeModel.getOneSentenceList()){
            String sentence = Html.fromHtml(wordPracticeModel.getSentence().replaceAll("<strong>","").replaceAll("</strong>","").replaceAll("</STRONG>","").replaceAll("<STRONG>","")).toString()+".rgb";
//            sentence= Html.fromHtml(sentence).toString().replaceAll("&\\*","&@&@&@&@&@&@&@&@&@&@");
            String[] sentenceSplit = sentence.split("&\\*");
            //构造训练全新数据结构
            SentenceLineTrainBean sentenceLineTrainBean=new SentenceLineTrainBean();
            //中文翻译
            sentenceLineTrainBean.setChineseSen(wordPracticeModel.getTranslate());
            List<SentenceTrainBean> sentenceTrainBeanList=new ArrayList<>();
            for (int i = 0; i < sentenceSplit.length; i++) {
                if(i==sentenceSplit.length-1){
                    if(TextUtils.equals("rgb",sentenceSplit[i].trim())){
                        continue;
                    }else{
                        sentenceSplit[i]=sentenceSplit[i].substring(0,sentenceSplit[i].length()-4);
                    }
                }

                String sentenceOutFill = sentenceSplit[i];
                if(!TextUtils.isEmpty(sentenceOutFill.trim())){
                    SentenceTrainBean sentenceTrainBean=new SentenceTrainBean();
                    sentenceTrainBean.setSentence(sentenceOutFill);
                    sentenceTrainBean.setFill(false);
                    setSentenceSplitToWord(sentenceOutFill,sentenceTrainBean);
                    sentenceTrainBeanList.add(sentenceTrainBean);
                }

                if(i!=sentenceSplit.length-1){
                    String sentenceFill=wordPracticeModel.getHideWordList().get(i).getWord();
                    SentenceTrainBean sentenceTrainBean2=new SentenceTrainBean();
                    sentenceTrainBean2.setSentence(sentenceFill);
                    sentenceTrainBean2.setFill(true);
                    sentenceTrainBean2.setFillIndex(startIndex);
                    sentenceTrainBeanList.add(sentenceTrainBean2);
                    startIndex++;
                    setSentenceSplitToWord(sentenceFill,sentenceTrainBean2);
                }
            }
            //训练内容
            sentenceLineTrainBean.setSentenceTrainBeanList(sentenceTrainBeanList);
            sentenceLineTrainBeanList.add(sentenceLineTrainBean);
        }

        SentenceTrainAdapter usageTrainAdapter = new SentenceTrainAdapter(getContext(),sentenceLineTrainBeanList);
        usageTrainAdapter.setShowCount(1);
        recyclerView.setAdapter(usageTrainAdapter);
    }

    private void setSentenceSplitToWord(String sentenceFill,SentenceTrainBean sentenceTrainBean){
        String[] fillSpilt = sentenceFill.split(" ");
        List<WordTrainBean> wordTrainBeanList=new ArrayList<>();
        if(fillSpilt.length>1){
            for (int j = 0; j < fillSpilt.length; j++) {
                String word=fillSpilt[j];
                if(TextUtils.isEmpty(word.trim())){
                    continue;
                }
                WordTrainBean wordTrainBean=new WordTrainBean();
                wordTrainBean.setWord(word);
                wordTrainBeanList.add(wordTrainBean);
            }
        }else{
            String word=sentenceFill;
            if(!TextUtils.isEmpty(word.trim())){
                WordTrainBean wordTrainBean=new WordTrainBean();
                wordTrainBean.setWord(word);
                wordTrainBeanList.add(wordTrainBean);
            }

        }
        sentenceTrainBean.setWordTrainBeanList(wordTrainBeanList);
    }

    @Override
    public void onClick(View v) {
        if(System.currentTimeMillis()-time<500){
            return;
        }
        time=System.currentTimeMillis();
        super.onClick(v);
        int id=v.getId();
        if(id==R.id.tv_i_know){
            closeAlertOperate();
        }
    }
}
